package com.hnhd.eorder.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.hnhd.eorder.R;

/**
 * 选台界面 对话框风格
 * 
 * @author dengbiao
 * 
 */
public class TableDialog extends Dialog {

	private Context context;

	/**
	 * 包厢餐台网格
	 */
	public GridView gv_table_list;

	/**
	 * 网格填充用的键值对的键数组
	 */
	public String[] from = { "tableName", "tableNum", "tableMaxNum",
			"tableWaiter" };

	/**
	 * 要填充的对象id数组
	 */
	public int[] to = { R.id.tv_table_item_name, R.id.tv_table_item_num,
			R.id.tv_table_item_max_num, R.id.tv_table_item_waiter };

	/**
	 * 包厢图片
	 */
	private ImageView iv_table_room;

	/**
	 * 包厢按钮组 --所有的包厢分类按钮都在group中
	 */
	private RadioGroup rg_room_group;

	/**
	 * 向前推移一个包厢
	 */
	private Button btn_table_pre;

	/**
	 * 向后推移一个包厢
	 */
	private Button btn_table_next;
	
	/**
	 * 刷新餐台按钮
	 */
	private Button btn_table_refresh;
	
	/**
	 * 返回按钮   --关闭选台界面
	 */
	private Button btn_table_back;

	public TableDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
		setContentView(R.layout.dialog_table);

		// 选台界面的餐台网格
		gv_table_list = (GridView) findViewById(R.id.gv_table_list);

		// 初始化数据
		this.initGridView(gv_table_list, from, to);

		// 包厢分类按钮
		RadioGroup rg_room_group = (RadioGroup) findViewById(R.id.rg_room_group);
		rg_room_group.setOnCheckedChangeListener(new RadioListener());

		// 包厢图片
		iv_table_room = (ImageView) findViewById(R.id.iv_table_room);

		//获得界面按钮对象  并绑定监听
		btn_table_pre = (Button) findViewById(R.id.btn_table_pre);
		btn_table_next = (Button) findViewById(R.id.btn_table_next);
		btn_table_refresh = (Button) findViewById(R.id.btn_table_refresh);
		btn_table_back = (Button) findViewById(R.id.btn_table_back);

		btn_table_pre.setOnClickListener(new BtnListener());
		btn_table_next.setOnClickListener(new BtnListener());
		btn_table_refresh.setOnClickListener(new BtnListener());
		btn_table_back.setOnClickListener(new BtnListener());

	}

	private class BtnListener implements android.view.View.OnClickListener {

		public void onClick(View v) {
			Button btn = (Button) v;
			int id = btn.getId();
			switch (id) {
			case R.id.btn_table_pre:
				Toast.makeText(getContext(), "pre", Toast.LENGTH_SHORT).show();
				break;
			case R.id.btn_table_next:
				Toast.makeText(getContext(), "next", Toast.LENGTH_SHORT).show();
				break;
			case R.id.btn_table_refresh:
				Toast.makeText(getContext(), "refresh", Toast.LENGTH_SHORT).show();
				break;
			case R.id.btn_table_back:
				TableDialog.this.cancel();
				break;
			}

		}
	}

	private class RadioListener implements OnCheckedChangeListener {

		public void onCheckedChanged(RadioGroup group, int checkedId) {

			switch (checkedId) {
			case R.id.rb_room_01:
				iv_table_room.setImageResource(R.drawable.room);
				initGridView(gv_table_list, from, to);
				break;
			case R.id.rb_room_02:
				iv_table_room.setImageResource(R.drawable.room2);
				initGridView2(gv_table_list, from, to);
				break;
			case R.id.rb_room_03:
				iv_table_room.setImageResource(R.drawable.room3);
				initGridView3(gv_table_list, from, to);
				break;
			}
		}
	}

	private void initGridView(GridView gridView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < 20; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(from[0], "101");
			map.put(from[1], "0");
			map.put(from[2], "6");
			map.put(from[3], "空闲");
			list.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_table_view, from, to);
		gridView.setAdapter(adapter);
	}

	private void initGridView2(GridView gridView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 5; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(from[0], "102");
			map.put(from[1], "0");
			map.put(from[2], "6");
			map.put(from[3], "空闲");
			list.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_table_view, from, to);
		gridView.setAdapter(adapter);
	}

	private void initGridView3(GridView gridView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 5; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put(from[0], "103");
			map.put(from[1], "0");
			map.put(from[2], "6");
			map.put(from[3], "空闲");
			list.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_table_view, from, to);
		gridView.setAdapter(adapter);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
